﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class actionButton : MonoBehaviour,IPointerClickHandler,IClickable, IPointerEnterHandler,IPointerExitHandler
{
    [SerializeField]
    private Image icon;
    public Image MyIcon { get => icon; set => icon=value; }
    public IUsable MyUsable { get; set; }
    public Button  MyButt { get; set; }
    private int usablesCount;
    public int MyCount => usablesCount;
    [SerializeField]
    private Text stackCountText;
    public Text MyStackSize => stackCountText;

    public Stack<IUsable> MyUsables
    {
        get => usables;
        set
        {
            if (value.Count > 0)
            {
                MyUsable = value.Peek();
            }
            else
            {
                MyUsable = null;
            }
            usables = value;

        } 
         
    }

    private Stack<IUsable> usables=new Stack<IUsable>();
    
    
    // Start is called before the first frame update
    void Start()
    {
        MyButt = GetComponent<Button>();
        MyButt.onClick.AddListener(OnClick);
        inventoryScript.MyInstance.itemCountEvent += new ItemCount(UpdateCount);
    }

    // Update is called once per frame
    void Update()
    {
      
    }
    public void OnClick()
    {
        
        if (handScript.MyInstance.MyMovable == null)
        {
            if (MyUsables != null && MyUsables.Count > 0)
            {
                MyUsables.Peek().use();
                
            }
            else if (MyUsable != null)
            {
                
                MyUsable.use();
                
            }
            
        }
       
    }
    public void OnPointerClick(PointerEventData eventData)
    {
        if (eventData.button == PointerEventData.InputButton.Left)
        {
            if(handScript.MyInstance.MyMovable != null && handScript.MyInstance.MyMovable is IUsable)
            {
                setUsable(handScript.MyInstance.MyMovable as IUsable);

            }
        }
    }
    public void setUsable(IUsable used)
    {
        if(used is Item)
        {
            MyUsables = inventoryScript.MyInstance.UsableItems(used);
            if (inventoryScript.MyInstance.FromSlot != null)
            {
                inventoryScript.MyInstance.FromSlot.MyIcon.color = Color.white;
                inventoryScript.MyInstance.FromSlot = null;
            }
        }
        else
        {
            MyUsables.Clear();
            this.MyUsable = used;
        }
        usablesCount = MyUsables.Count;
        changeView(used as IMovable);
        UIManager.MyInstance.refreshTooltip(MyUsable as IDescription);
    }
    public void changeView(IMovable movable)
    {
        if (handScript.MyInstance.MyMovable != null)
        {
            handScript.MyInstance.Drop();
        }
        MyIcon.sprite = movable.Icon;
        MyIcon.color = Color.white;
        if (usablesCount > 1 )
        {
            UIManager.MyInstance.UpdateStackSize(this);

        }
        else if (MyUsable is Spell)
        {
            UIManager.MyInstance.ClearStackSize(this);
        }

    }
    public void UpdateCount(Item itm)
    {
       if(itm is IUsable)
        {
            if (MyUsables.Count > 0 && (MyUsables.Peek() as Item).MyTitle==itm.MyTitle)
            {
                MyUsables = inventoryScript.MyInstance.UsableItems(itm as IUsable);
                usablesCount = MyUsables.Count;
                UIManager.MyInstance.UpdateStackSize(this);
            }
            
        }
    }
    public void OnPointerEnter(PointerEventData eventData)
    {
        IDescription temp=null;
        //Stack<IDescription> temps;
       if (MyUsable !=null&&MyUsable is IDescription)
        {
            temp = (IDescription)MyUsable;

            //UIManager.MyInstance.showToolTip(transform.position);
        }
        else if(MyUsables.Count > 0 && MyUsables.Peek() is IDescription)
        {
            temp = (IDescription)MyUsables.Peek();
           
        }
        if (temp != null)
        {
            UIManager.MyInstance.showToolTip(new Vector2(1,0),transform.position,temp);
        }
    }

    public void OnPointerExit(PointerEventData eventData)
    {

        UIManager.MyInstance.hideToolTip();
    }


}
